﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IntroduceCompany.Util;
using System.Web.UI.WebControls;
using System.IO;

namespace IntroduceCompany.Models
{
    public class GeneralModels
    {

        private ThumbnailClass _thumbnailClass = new ThumbnailClass();

        /// <summary>
        /// Upload Icon Image 
        /// </summary>
        /// <param name="uploadFile"></param>
        /// <param name="physicalApplicationPath"></param>
        /// <param name="oldImage"></param>
        /// <returns></returns>
        public String MenuListUploadImage(HttpPostedFileBase uploadFile, String physicalApplicationPath, String oldImage)
        {

            string imageResult = "";
            if (uploadFile.FileName.Length > 0)
            {
                imageResult = _thumbnailClass.UploadImage2(uploadFile, physicalApplicationPath + "Content/Backend/images/", 30, oldImage
                    , (Path.GetFileNameWithoutExtension(uploadFile.FileName).ToLower().Trim()+"-"+DateTime.Now.Ticks) );
            }
            return imageResult;
        }


        public String LanguageUploadImage(HttpPostedFileBase uploadFile, String physicalApplicationPath, String oldImage)
        {

            string imageResult = "";
            if (uploadFile.FileName.Length > 0)
            {
                imageResult = _thumbnailClass.UploadImage2(uploadFile, physicalApplicationPath + "Image/Flag/", 500, oldImage
                    , (Path.GetFileNameWithoutExtension(uploadFile.FileName).ToLower().Trim() + "-" + DateTime.Now.Ticks));
            }
            return imageResult;
        }

        /// <summary>
        /// Auto resize horizontal image and upload to server
        /// </summary>
        /// <param name="uploadFile"></param>
        /// <param name="fullpath"></param>
        /// <param name="oldImageName"></param>
        /// <param name="newImageName"></param>
        /// <param name="newHeight"></param>
        /// <returns></returns>
        public String UploadImage(HttpPostedFileBase uploadFile, String fullpath, String oldImageName, String newImageName, int newHeight)
        {
            newImageName = ThumbnailClass.ImageNameStandardization(newImageName);
            newImageName = newImageName + "-" + DateTime.Now.Ticks;
            newImageName = newImageName.Length > 490 ? newImageName.Substring(0, 70) : newImageName;
            string imageNameResult = "";
            if (uploadFile.FileName.Length > 0)
            {
                imageNameResult = _thumbnailClass.UploadImage1(uploadFile, fullpath, newHeight, oldImageName, newImageName);
            }
            return imageNameResult;
        }

        /// <summary>
        /// Auto resize horizontal image and upload to server for album create
        /// </summary>
        /// <param name="uploadFile"></param>
        /// <param name="path"></param>
        /// <param name="albumID"></param>
        /// <param name="oldImageName"></param>
        /// <param name="newImageName"></param>
        /// <param name="newHeight"></param>
        /// <returns></returns>
        public String UploadImageAlbum(HttpPostedFileBase uploadFile, String path, int albumID, String oldImageName, String newImageName, int newHeight)
        {
            CreateAlbumFolder(albumID.ToString(), path);
            path = path + "/album-" + albumID.ToString()+"/";

            string imageNameResult = "";

            if (uploadFile.FileName.Length > 0)
            {
                imageNameResult = _thumbnailClass.UploadImage1(uploadFile, path, newHeight, oldImageName, newImageName);
            }
            return imageNameResult;
        }

        /// <summary>
        /// Auto resize horizontal image and upload to server
        /// </summary>
        /// <param name="uploadFile"></param>
        /// <param name="fullpath"></param>
        /// <param name="oldImageName"></param>
        /// <param name="newImageName"></param>
        /// <param name="newHeight"></param>
        /// <returns></returns>
        public String UploadImage2(HttpPostedFileBase uploadFile, String fullpath, String oldImageName, String newImageName, int newHeight)
        {
            string imageNameResult = "";
            if (uploadFile.FileName.Length > 0)
            {
                imageNameResult = _thumbnailClass.UploadImage2(uploadFile, fullpath, newHeight, oldImageName, newImageName);
            }
            return imageNameResult;
        }

        /// <summary>
        /// create necessary folder for an album
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="locationpath"></param>
        private void CreateAlbumFolder(String productID, String locationpath)
        {
            // Specify a "currently active folder" 
            string folderAlbum = "album-" + productID;
            string newfolderAlbumPath = System.IO.Path.Combine(locationpath, folderAlbum);

            // Create the folder for album :  ../images/album/album-productID
            System.IO.Directory.CreateDirectory(newfolderAlbumPath);

            // Create the folder "photo" in album folder : ../images/album/album-productID/photo
            string newPhotoPath = System.IO.Path.Combine(newfolderAlbumPath, "photo");
            System.IO.Directory.CreateDirectory(newPhotoPath);

            // Create the folder "thumbnail" in album/photo folder : ../images/album/album-productID/photo/
            string newThumbnialPath = System.IO.Path.Combine(newPhotoPath, "thumbnail");
            System.IO.Directory.CreateDirectory(newThumbnialPath);
        }



        
    }
}